package com.wontube.smr.web.controller.i18n;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.model.po.TVEpisode;
import com.wontube.smr.client.model.po.TVShow;
import com.wontube.smr.client.model.vo.TVShowItems;
import com.wontube.smr.web.client.IAnimeWebHandler;
import com.wontube.smr.web.client.model.bo.QueryItemParams;

/**
 * 动漫控制器
 * 
 * @author luobiao@wondershare.cn
 * 
 */
@Controller
@RequestMapping("/world/anime")
public class I18nAnimeController {
	private final static int PAGE_SIZE = 30;	
	
	@RequestMapping("")
	public void f1(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/world/anime/");		
	}

	@RequestMapping("/")
	public ModelAndView animeAll() {
		//ModelAndView mv = new ModelAndView("i18n/anime/anime_index");
		ModelAndView mv = getListAnimeView(new QueryItemParams(true),"i18n/anime/anime_index");
		return mv;
	}
	
	@RequestMapping("/all")
	public ModelAndView animeAlls()	{
		ModelAndView mv = new ModelAndView("i18n/anime/anime_index");
		mv.addObject("animeGenres", getAnimeWebHandler().listAnimeGenres());
		return mv;
	}
	
	@RequestMapping("/all/query")
	public ModelAndView queryAnimes(QueryItemParams params){
		params.setNeedGenres(false);
		return getListAnimeView(params, "i18n/anime/anime_list");
	}
	
	@RequestMapping("/genre/{genreName}")
	public ModelAndView getAnimeByGenre(@PathVariable("genreName") String genreName){
		QueryItemParams params = new QueryItemParams();
		params.setGenre(genreName);
		params.setNeedGenres(true);
		ModelAndView mv = getListAnimeView(params, "i18n/anime/anime_index");
		return mv;
	}
	
	@RequestMapping("/alpha/{alphaValue}")
	public ModelAndView getAnimeByAlpha(@PathVariable("alphaValue") String alphaValue){
		QueryItemParams params = new QueryItemParams();
		params.setInitial(alphaValue);
		params.setNeedGenres(true);
		ModelAndView mv = getListAnimeView(params, "i18n/anime/anime_index");
		return mv;
	}
	
	@RequestMapping("/{id}")
	public ModelAndView getAnimeDetail(@PathVariable("id") String showId,
			HttpServletResponse response) throws IOException{
		ModelAndView mv = new ModelAndView();
		mv.setViewName("i18n/anime/detail/anime_detail_index");
		IAnimeWebHandler animeWebHandler = getAnimeWebHandler();
		Map<String, Object> maps = animeWebHandler.collectTVDetails(showId);
		TVShow show = (TVShow)maps.get("show");
		if(show == null){
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		}
		
		mv.addAllObjects(maps);
		return mv;
	}
	
	@RequestMapping("/watch-{id}")
	public ModelAndView playAnime(@PathVariable("id") String epId){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("i18n/anime/play/anime_play_index");
		IAnimeWebHandler animeWebHandler = getAnimeWebHandler();		
		Map<String, Object> maps = animeWebHandler.collectTVPlayDetail(epId);
		TVEpisode ep = (TVEpisode)maps.get("anime");
		if(ep != null){
			String url = ep.getUrl();
			String[] urlAry = url.split("-");		
			mv.addObject("mediaid",urlAry[urlAry.length-1]);
		}
		
		mv.addAllObjects(maps);
		return mv;
	}
	
	@RequestMapping("/relate/{showId}")
	public ModelAndView getRelateAnime(@PathVariable("showId") String showId){
		ModelAndView mv = new ModelAndView("i18n/anime/anime_relate");
		IAnimeWebHandler animeWebHandler = getAnimeWebHandler();
		TVShowItems items = animeWebHandler.selectRelatedAnime(showId, new PagenateInfo(1, 6));
		List<TVShow> list = new ArrayList<TVShow>();
		for (TVShow tvshow : items.getItems()) {
			if (!tvshow.getId().equals(showId) && list.size() < 5) {
				list.add(tvshow);
			}
		}
		items.setItems(list);
		mv.addObject("showItems", items);
		return mv;
	}
	
	@RequestMapping("/simulacasts")
	public void f2(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/world/simulacasts/");		
	}
	
	@RequestMapping("/simulacasts/")
	public ModelAndView simulacasets(){
		ModelAndView mv = getSimulaView(1, "i18n/anime/anime_index");
		mv.addObject("category", "simulacasts");
		return mv; 
	}
	
	@RequestMapping("/simulacasts/query")
	public ModelAndView listSimulacasts(@RequestParam("page") Integer page){
		return getSimulaView(page, "i18n/anime/anime_list");
	}
	
	@RequestMapping("/updated")
	public void f3(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/world/updated/");		
	}
	
	@RequestMapping("/updated/")
	public ModelAndView recent(){
		ModelAndView mv = getRecentView(1, "i18n/anime/anime_index");
		mv.addObject("category", "updated");
		return mv; 
	}
	
	@RequestMapping("/updated/query")
	public ModelAndView listRecents(@RequestParam("page") Integer page){
		return getRecentView(page, "i18n/anime/anime_list");
	}
	
	private ModelAndView getListAnimeView(QueryItemParams params, String viewName){
		ModelAndView mv = new ModelAndView(viewName);
		IAnimeWebHandler animeWebHandler = getAnimeWebHandler();
		TVShowItems animeItems = animeWebHandler.listAnimes(params, params.getPageInfo(PAGE_SIZE));
		if(animeItems != null){
			if(animeItems.getItems().size() == PAGE_SIZE){
				mv.addObject("hasMore", true);
			}
			if(params.isNeedGenres()){
				mv.addObject("animeGenres", animeItems.getTvGenres());
			}		
		}
		mv.addObject("animeItems",animeItems);		
		return mv;
	}
	
	private ModelAndView getRecentView(Integer page, String viewName){
		ModelAndView mv = new ModelAndView(viewName);
		IAnimeWebHandler handler = getAnimeWebHandler();
		TVShowItems animeItems = handler.listRecentAinmes(new PagenateInfo(page, PAGE_SIZE));
		if(animeItems != null){
			if(animeItems.getItems().size() == PAGE_SIZE){
				mv.addObject("hasMore", true);
			}			
		}
		mv.addObject("animeItems",animeItems);		
		return mv;
	}
	
	private ModelAndView getSimulaView(Integer page, String viewName){
		ModelAndView mv = new ModelAndView(viewName);
		IAnimeWebHandler handler = getAnimeWebHandler();
		TVShowItems animeItems = handler.listAnimeSimulacasts(new PagenateInfo(page, PAGE_SIZE));
		if(animeItems != null){
			if(animeItems.getItems().size() == PAGE_SIZE){
				mv.addObject("hasMore", true);
			}			
		}
		mv.addObject("animeItems",animeItems);		
		return mv;
	}	

	private IAnimeWebHandler getAnimeWebHandler() {
		Remoting<IAnimeWebHandler> remoting = new Remoting<IAnimeWebHandler>();
		return remoting.createService(IAnimeWebHandler.class);
	}

}
